package com.mbbb.yygh.hosp.testmongo;

import com.mbbb.yygh.common.result.R;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Pattern;

@Api(tags = "mongodb接口1")
@RestController
@RequestMapping("/mongo1")
public class TestMongo1 {

    @Autowired
    private MongoTemplate mongoTemplate;

    //添加
    @ApiOperation("添加")
    @PostMapping("/add")
    public R addMongo() {
        User user = new User();
        user.setName("小含含");
        user.setAge(19);
        user.setEmail("131354212@qq.com");
        User save = mongoTemplate.save(user);
        return R.ok().data("user",save);
    }

    //查询所有
    @ApiOperation("查询所有")
    @GetMapping("/findAll")
    public R findAll() {
        List<User> list = mongoTemplate.findAll(User.class);
        return R.ok().data("list",list);
    }

    //根据id查询
    @ApiOperation("根据id查询")
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable String id) {
        User user = mongoTemplate.findById(id,User.class);
        return R.ok().data("user",user);
    }

    //条件查询
    @ApiOperation("条件查询")
    @GetMapping("/findUser")
    public R findUser() {
        Query query = new Query(Criteria
                                    .where("name").is("小琦琦")
                                    .and("age").is(20));
        List<User> list = mongoTemplate.find(query, User.class);
        return R.ok().data("list",list);
    }

    //模糊查询
    @ApiOperation("模糊查询")
    @GetMapping("/findLike")
    public R findLike() {
        String name = "小";
        String format = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(format, Pattern.CASE_INSENSITIVE);
        Query query = new Query(Criteria.where("name").regex(pattern));
        List<User> list = mongoTemplate.find(query, User.class);
        return R.ok().data("list",list);
    }

    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("/findPage")
    public R findUsersPage() {
        int pageNow = 1;
        int pageSize = 1;
        String name = "小";

        Query query = new Query();
        String format = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(format, Pattern.CASE_INSENSITIVE);
        query.addCriteria(Criteria.where("name").regex(pattern));

        long count = mongoTemplate.count(query, User.class);
        List<User> list = mongoTemplate.find(query.skip((pageNow - 1) * pageSize).limit(pageSize), User.class);

        return R.ok().data("count",count).data("list",list);
    }

    //根据id修改
    @ApiOperation("根据id修改")
    @GetMapping("/update/{id}")
    public R updateUser(@PathVariable String id) {
        User user = mongoTemplate.findById(id, User.class);
        user.setName("熊二");
        user.setAge(38);

        Query query = new Query(Criteria.where("_id").is(user.getId()));
        Update update = new Update();
        update.set("name",user.getName());
        update.set("age",user.getAge());

        UpdateResult result = mongoTemplate.upsert(query, update, User.class);
        long count = result.getModifiedCount();

        return R.ok().data("result",result).data("count",count);
    }

    //根据id删除
    @ApiOperation("根据id删除")
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        User user = mongoTemplate.findById(id, User.class);

        Query query = new Query(Criteria.where("_id").is(user.getId()));
        DeleteResult result = mongoTemplate.remove(query, User.class);
        long count = result.getDeletedCount();

        return R.ok().data("result",result).data("count",count);
    }
}
